package com.elawlink.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.elawlink.util.DateUtil;
import com.elawlink.word.WordToPDF;
 
/**
 * @Author : wuyongde
 * @CreateTime : 2021/5/27
 * @Description :
 **/
@RestController
public class WordPdfController {
 
    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法
 
    @GetMapping("/wordToPdf")
    public String sendDirectMessage(@RequestParam(value = "path", required = true) String path) {
    	
    	System.out.println("开始转换："+DateUtil.getSQLDate()+">path="+path);
    	
		String outpath = path.replace(".docx", ".pdf");

		InputStream source;
		OutputStream target;
		try {
			source = new FileInputStream(path);
			target = new FileOutputStream(outpath);
			Map<String, String> params = new HashMap<String, String>();

			PdfOptions options = PdfOptions.create();

			WordToPDF.wordConverterToPdf(source, target, options, params);
			
			File file = new File(outpath);
			if(file.length() == 0){
				 return "fail";
			}
			
			System.out.println("转换成功："+DateUtil.getSQLDate()+">outpath="+outpath);
	        return "success";
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("转换失败："+DateUtil.getSQLDate()+">outpath="+outpath);
		
        return "fail";
		
    }

    public static void main(String[] args) {
    	System.out.println("/mnt/saas/icriszip/202105311429303906//2711304_PCM Holding Limited/document_index.docx".replace(" ","%20"));
	}
 
}